package com.bjsxt.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bjsxt.domain.SysUserRole;
import com.bjsxt.service.SysRoleService;
import com.bjsxt.service.SysUserRoleService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bjsxt.mapper.SysUserMapper;
import com.bjsxt.domain.SysUser;
import com.bjsxt.service.SysUserService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService{

        @Autowired
        private SysUserRoleService sysUserRoleService;



    @Override
    public Page<SysUser> findByPage(Page<SysUser> page, String mobile, String fullname) {
       Page<SysUser>pageData =  page(page,new LambdaQueryWrapper<SysUser>().like(!StringUtils.isEmpty(mobile),SysUser::getMobile,mobile)
                                                        .like(!StringUtils.isEmpty(fullname),SysUser::getFullname,fullname));
        List<SysUser> records = pageData.getRecords();
        if(!CollectionUtils.isEmpty(records)){
            for (SysUser record : records){
               List<SysUserRole>userRoles =  sysUserRoleService.list(new LambdaQueryWrapper<SysUserRole>().eq(SysUserRole::getUserId,record.getId()));
                if(!CollectionUtils.isEmpty(userRoles)){
                    record.setRole_strings(userRoles.stream()
                            .map(sysUserRole -> sysUserRole.getRoleId().toString()).collect(Collectors.joining(",")));
                }
            }
        }
        return pageData;
    }

    @Override
    @Transactional
    public boolean addUser(SysUser sysUser) {
        String password = sysUser.getPassword();
        String role_strings = sysUser.getRole_strings();
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        String encode = bCryptPasswordEncoder.encode(password);
        sysUser.setPassword(encode);
        boolean save = super.save(sysUser);
        System.out.println("XCXXXXXXXXXX");
        System.out.println(sysUser.getId());
        System.out.println(sysUser.toString());


        if(save){
            // 给用户新增角色数据
            if(!StringUtils.isEmpty(role_strings)){
                String[] roleIds = role_strings.split(",");
                List<SysUserRole> sysUserRoleList = new ArrayList<>(roleIds.length) ;
                for (String roleId : roleIds) {
                    SysUserRole sysUserRole = new SysUserRole();
                    sysUserRole.setRoleId(Long.valueOf(roleId));
                    sysUserRole.setUserId(sysUser.getId());
                    sysUserRoleList.add(sysUserRole) ;
                }
                sysUserRoleService.saveBatch(sysUserRoleList) ;
            }
        }

        return save;
    }

    @Override
    public boolean updateUser(SysUser sysUser) {
        String password = sysUser.getPassword();
        String role_string = sysUser.getRole_strings();
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        String encode = bCryptPasswordEncoder.encode(password);
        sysUser.setPassword(encode);

        boolean ok = super.updateById(sysUser);
        if(ok){
            //角色数据新增
            if(StringUtils.isEmpty(role_string)){
                String[] roleIds = role_string.split(",");
                List<SysUserRole>sysUserRoleList = new ArrayList<>(roleIds.length);
                for(String roleId: roleIds){
                    SysUserRole sysUserRole = new SysUserRole();
                    sysUserRole.setRoleId(Long.valueOf(roleId));
                    sysUserRole.setUserId(sysUser.getId());
                    sysUserRoleList.add(sysUserRole);
                }
                sysUserRoleService.updateBatchById(sysUserRoleList);


            }


        }

        return ok;


    }


    @Override
    public boolean removeByIds(Collection<? extends Serializable> idList) {
        boolean b = super.removeByIds(idList);
        sysUserRoleService.remove(new LambdaQueryWrapper<SysUserRole>().in(SysUserRole::getUserId,idList));
        return  b;
    }
}
